/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.system.service.impl;

import me.zhengjie.modules.system.domain.BadgeManage;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.repository.BadgeManageRepository;
import me.zhengjie.modules.system.service.BadgeManageService;
import me.zhengjie.modules.system.service.dto.BadgeManageDto;
import me.zhengjie.modules.system.service.dto.BadgeManageQueryCriteria;
import me.zhengjie.modules.system.service.mapstruct.BadgeManageMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://docs.auauz.net
* @description 服务实现
* @author 黄星星
* @date 2020-05-30
**/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class BadgeManageServiceImpl implements BadgeManageService {

    private final BadgeManageRepository badgeManageRepository;
    private final BadgeManageMapper badgeManageMapper;

    @Override
    public Map<String,Object> queryAll(BadgeManageQueryCriteria criteria, Pageable pageable){
        Page<BadgeManage> page = badgeManageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(badgeManageMapper::toDto));
    }

    @Override
    public List<BadgeManageDto> queryAll(BadgeManageQueryCriteria criteria){
        return badgeManageMapper.toDto(badgeManageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public BadgeManageDto findById(Integer id) {
        BadgeManage badgeManage = badgeManageRepository.findById(id).orElseGet(BadgeManage::new);
        ValidationUtil.isNull(badgeManage.getId(),"BadgeManage","id",id);
        return badgeManageMapper.toDto(badgeManage);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public BadgeManageDto create(BadgeManage resources) {
        resources.setCid(0l);
        return badgeManageMapper.toDto(badgeManageRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(BadgeManage resources) {
        BadgeManage badgeManage = badgeManageRepository.findById(resources.getId()).orElseGet(BadgeManage::new);
        ValidationUtil.isNull( badgeManage.getId(),"BadgeManage","id",resources.getId());
        badgeManage.copy(resources);
        badgeManageRepository.save(badgeManage);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            badgeManageRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<BadgeManageDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (BadgeManageDto badgeManage : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("创建时间", badgeManage.getCreateTime());
            map.put("更新时间", badgeManage.getUpdateTime());
            map.put("创建者", badgeManage.getCreateBy());
            map.put("更新者", badgeManage.getUpdateBy());
            map.put("绑定状态code", badgeManage.getBindStatusCode());
            map.put("胸卡卡号", badgeManage.getBadgeNum());
            map.put("当前电量", badgeManage.getCurrentBattery());
            map.put("绑定人员id", badgeManage.getBindUserId());
            map.put("绑定人员姓名", badgeManage.getBindUserName());
            map.put("绑定状态名称", badgeManage.getBindStatusName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}